//
//  YCMyQRCodeViewController.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/12.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCMyQRCodeViewController.h"
#import "YCNavigationViewController.h"
#import <FXBlurView/FXBlurView.h>
#import "YCQRCodeGenerator.h"
#import "YCQRCodeView.h"

@interface YCMyQRCodeViewController ()<UIImagePickerControllerDelegate>

@property (nonatomic, weak) UIImageView *bgImageIVew;
@property (nonatomic, weak) UIButton *saveButton;
@property (nonatomic, weak) UIButton *relayButton;
@property (nonatomic, weak) YCQRCodeView *qrCodeView;
@end

@implementation YCMyQRCodeViewController

#pragma mark - 懒加载
- (UIImageView *)bgImageIVew
{
    if (!_bgImageIVew) {
        
        UIImageView *bgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        bgView.image = [UIImage imageNamed:@"mf_bg_img"];
        bgView.userInteractionEnabled = YES;
        
        FXBlurView * aview = [[FXBlurView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        aview.tintColor = [UIColor clearColor];
        aview.blurRadius = 10;
        [bgView addSubview:aview];
        
        [self.view addSubview:bgView];
        _bgImageIVew = bgView;
        
    }
    return _bgImageIVew;
}

- (YCQRCodeView *)qrCodeView
{
    if (!_qrCodeView) {
        YCQRCodeView *qrView = [[YCQRCodeView alloc] init];
        [self.view addSubview:qrView];
        _qrCodeView = qrView;
    }
    return _qrCodeView;
}

- (UIButton *)saveButton
{
    if (!_saveButton) {
        UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        saveButton.titleLabel.font = [UIFont systemFontOfSize:14];
        saveButton.layer.borderColor = [UIColor yc_colorWithUInt:0x808080 alpha:0.8].CGColor;
        saveButton.layer.borderWidth = 1;
        [saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [saveButton setTitleColor:[UIColor yc_colorWithUInt:0xffffff alpha:0.7] forState:UIControlStateNormal];
        saveButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        saveButton.layer.cornerRadius = 15;
        saveButton.layer.masksToBounds = YES;
        [saveButton addTarget:self action:@selector(didTouchUpInsideSaveButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:saveButton];
        _saveButton = saveButton;
    }
    return _saveButton;
}

- (UIButton *)relayButton
{
    if (!_relayButton) {
        UIButton *relayButton = [UIButton buttonWithType:UIButtonTypeCustom];
        relayButton.titleLabel.font = [UIFont systemFontOfSize:14];
        relayButton.layer.borderColor = [UIColor yc_colorWithUInt:0x808080 alpha:0.8].CGColor;
        relayButton.layer.borderWidth = 1;
        [relayButton setTitle:@"转发" forState:UIControlStateNormal];
        [relayButton setTitleColor:[UIColor yc_colorWithUInt:0xffffff alpha:0.7] forState:UIControlStateNormal];
        relayButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        relayButton.layer.cornerRadius = 15;
        relayButton.layer.masksToBounds = YES;
        [relayButton addTarget:self action:@selector(didTouchUpInsideRelayButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:relayButton];
        _relayButton = relayButton;
    }
    return _relayButton;
}


#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    [self yc_addBackBarButtonItem];
    //    self.navigationItem.title = @"我的二维码";//默认是黑的
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"我的二维码";
    [titleLabel sizeToFit];
    self.navigationItem.titleView = titleLabel;
    [self yc_addRightBarButtonItemWithImageNamed:@"nav_More_white" target:self action:@selector(rightBarButtonItemClick:)];
    [self layout];
    // 生成二维码
    NSString *string = @"www.baidu.com";
    UIImage *qrImage = [YCQRCodeGenerator qrImageForString:string imageSize:180];
    self.qrCodeView.codeImageView.image = qrImage;
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    YCNavigationViewController *navVc = (YCNavigationViewController *)self.navigationController;
    [navVc setNavigationBarClearColor];
    [navVc.navigationBar setTintColor:[UIColor whiteColor]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    YCNavigationViewController *navVc = (YCNavigationViewController *)self.navigationController;
    [navVc setNavigationBarResume];
    
}

- (void)layout
{
    [self.bgImageIVew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.qrCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).offset(-7);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
    }];
    
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.bottom.equalTo(self.view).offset(-45);
        [YCAdapter performOtherBlock:^{
            make.width.equalTo(@100);
        } IPhone6Block:^{
            make.width.equalTo(@138);
        } IPhone6pBlock:^{
            make.width.equalTo(@140);
        }];
        make.height.equalTo(@32);
    }];
    
    [self.relayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-30);
        make.bottom.height.width.equalTo(self.saveButton);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



#pragma mark - 点击事件

- (void)rightBarButtonItemClick:(UIBarButtonItem *)barButton
{
    YCLogFunc
    
}

- (void)didTouchUpInsideRelayButton:(UIButton *)button
{
    YCLogFunc
    
}

- (void)didTouchUpInsideSaveButton:(UIButton *)button
{
    UIImage *saveImage = self.qrCodeView.codeImageView.image;
    UIImageWriteToSavedPhotosAlbum(saveImage, self, @selector(image:didFinishSavingWithError: contextInfo:), nil);
}

#pragma mark - 保存照片完成时方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}


@end
